package com.vms.data;

import java.util.List;

import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class GSMNetworkInfo extends PhoneStateListener {
	public static GSMNetworkInfo instance;
	TelephonyManager mTelephonyManager;
	private int mSignalStrengt = 0;
	private String evdo = "";
	private String ecio = "";
	private String snr = "";
	private String biterrorrate = "";
	
	private GSMNetworkInfo(TelephonyManager aTelephonyManager) {
		this.mTelephonyManager = aTelephonyManager;
		mTelephonyManager.listen(this, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
	}

	public String getIMEI() {
		return mTelephonyManager.getDeviceId();
	}

	public int getCellID() {
		try {
			GsmCellLocation loc = (GsmCellLocation) mTelephonyManager.getCellLocation();
			return Integer.parseInt(convertUTRAN(loc.getCid() + ""));
		} catch (Exception e) {
			return -1;
		}
	}

	public static String convertUTRAN(String cellid) {
		cellid = Integer.toHexString(Integer.parseInt(cellid));
		try {
			if (cellid.length() == 5)
				return cellid;

			String cell = cellid.substring(cellid.length() - 4);
			return "" + Integer.parseInt(cell, 16);
		} catch (Exception e) {
			return null;
		}
	}

	public int getLAC() {
		try {
			GsmCellLocation loc = (GsmCellLocation) mTelephonyManager.getCellLocation();
			return loc.getLac();
		} catch (Exception e) {
			return -1;
		}
	}

	public boolean isRoaming() {
		return mTelephonyManager.isNetworkRoaming();
	}

	public String getNetworkType() {
		int type = mTelephonyManager.getNetworkType();
		switch (type) {
		case TelephonyManager.NETWORK_TYPE_UNKNOWN:
			return "";
		case TelephonyManager.NETWORK_TYPE_GPRS:
			return "GPRS";
		case TelephonyManager.NETWORK_TYPE_EDGE:
			return "EDGE";
		case TelephonyManager.NETWORK_TYPE_UMTS:
			return "UMTS";
		case TelephonyManager.NETWORK_TYPE_HSDPA:
			return "HSDPA";
		case TelephonyManager.NETWORK_TYPE_HSUPA:
			return "HSUPA";
		case TelephonyManager.NETWORK_TYPE_HSPA:
			return "HSPA";
		case TelephonyManager.NETWORK_TYPE_CDMA:
			return "CDMA";
		case TelephonyManager.NETWORK_TYPE_EVDO_0:
			return "EVDO_0";
		case TelephonyManager.NETWORK_TYPE_EVDO_A:
			return "EVDO_A";
		case TelephonyManager.NETWORK_TYPE_1xRTT:
			return "1xRTT";
		case TelephonyManager.NETWORK_TYPE_IDEN:
			return "IDEN";
		}
		return "UNKNOW";
	}

	public int getSignalStreght() {
		try {
			//Return as dbm
			return mSignalStrengt* 2 - 113;
		} catch (Exception e) {
			return 0;
		}
	}

	public String getNetworkOperator() {
		return mTelephonyManager.getNetworkOperatorName();
	}

	@Override
	public void onSignalStrengthChanged(int asu) {
		Log.d("SIGNAL", "#1. " + String.valueOf(asu));
		if (mSignalStrengt != asu) {
			mSignalStrengt = asu;
		}
		super.onSignalStrengthChanged(asu);
	}

	@Override
	public void onSignalStrengthsChanged(SignalStrength signalStrength) {
		Log.d("SIGNAL", "#2.");

		if (signalStrength.isGsm())
			mSignalStrengt = signalStrength.getGsmSignalStrength();
		else {
			int strength = -1;
			if (signalStrength.getEvdoDbm() < 0)
				strength = signalStrength.getEvdoDbm();
			else if (signalStrength.getCdmaDbm() < 0)
				strength = signalStrength.getCdmaDbm();

			if (strength < 0) {
				// convert to asu
				mSignalStrengt = Math.round((strength + 113f) / 2f);
			}

		}
		
		evdo = signalStrength.getEvdoDbm() + "";
		ecio = signalStrength.getEvdoEcio() + "";
		snr = signalStrength.getEvdoSnr() + "";
		biterrorrate = signalStrength.getGsmBitErrorRate() + "";
		
		super.onSignalStrengthsChanged(signalStrength);
	}

	public String getMNC(){
		try {
			String networkOperator = mTelephonyManager.getNetworkOperator();
			return networkOperator.substring(3);
		} catch (Exception e) {
			return "";
		}
	}
	public String getMCC(){
		try {
			String networkOperator = mTelephonyManager.getNetworkOperator();
			return networkOperator.substring(0,3);
		} catch (Exception e) {
			return "";
		}
	}
	
	public String getEVDO(){
		return evdo;
	}
	
	public String getECIO(){
		return ecio;
	}
	
	public String getSNR(){
		return snr;
	}
	
	public String getBitErrorRate(){
		return biterrorrate;
	}
	
	public String getNeighbourCells(){
		StringBuffer buff = new StringBuffer();
		List<NeighboringCellInfo> cells = mTelephonyManager.getNeighboringCellInfo();
		
		for (NeighboringCellInfo cell : cells){
			buff.append(convertUTRAN(cell.getCid() + "")).append("(").append(cell.getRssi() * 2 - 113).append(")");
			if(cells.indexOf(cell) < cells.size() - 1)
				buff.append(";");
		}
		
		return buff.toString();
	}
	
	public static GSMNetworkInfo getInstance(TelephonyManager aTelephoneManager) {
		if (aTelephoneManager != null) {
			instance = new GSMNetworkInfo(aTelephoneManager);
		}
		return instance;
	}
}
